$(function(){
	comType();
});

function comType(){
	$("#snTypeId").html("");
	$("#typeSelect").html("");
	var url="../../comtype/query.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#snTypeId").append("<option  value=''>====请选择===</option>");
		$("#typeSelect").append("<option value=''>====请选择===</option>");
		$.each(mes,function(index,item){
			$("#snTypeId").append("<option  value="+item.typeId+">"+item.typeName+"</option>");
			$("#typeSelect").append("<option value="+item.typeId+">"+item.typeName+"</option>");
			load();
		});
	},"json");
}

function exit(){
	$("#typeId").val("");
	$("#snTypeId").val("");
	$("#typeName").val("");
	load();
	//关闭左右页面层
	layer.closeAll('page');
}
layui.use(['form', 'layedit', 'laydate','table','element', 'tree', 'layer', 'form', 'upload'], function(){
  var table = layui.table
  ,laydate = layui.laydate
  ,form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,layer = layui.layer
  ,$ = layui.jquery
  ,tree = layui.tree;
  
  
  
  //日期
  laydate.render({
    elem: '#date'
  });
  
  laydate.render({
	    elem: '#time'
  });
  
//日期时间选择器
  laydate.render({
    elem: '#opDate'
    ,type: 'datetime'
  });
  
//日期时间选择器
  laydate.render({
    elem: '#opTime'
    ,type: 'datetime'
  });
  //监听工具条
  table.on('tool(demo)', function(obj){
    var da = obj.data;
    
    if(obj.event === 'edit'){
    	var url="../../comtype/findById.action";
        var data={"typeId":da.typeId};
        $.post(url,data,function(mes){
        	layer.open({
    	        type: 1	 //此处以iframe举例
    	        ,title: '数据修改'
    	        ,area: ['550px', '350px']
        		,offset: 'auto'
    	        ,content:$('#addOrUpdate')
    	        ,success: function(layero){
    	        	$("#typeId").val(mes.typeId);
    	        	$("#snTypeId").val(mes.snTypeId);
    	        	$("#typeName").val(mes.typeName);
    	        	//渲染
    	        	load();
        		},end : function() {
    	        	//弹框关闭的回调函数
    				$("#addOrUpdate").hide();
    				exit();
    			}
    	    });
        	
        },"json");
    }else if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
        	var url="../../comtype/delete.action";
        	var data={"typeId":da.typeId}
        	$.post(url,data,function(mes){
        		if(mes==1){
        			obj.del();
                    layer.close(index);
                    layer.msg("删除成功");
        		}else{
        			layer.msg("删除失败");
        		}
        	},"json");
            
          });
        }
  });
  
  
  var $ = layui.$, active = {
		    reload: function(){
		      var typeSelect = $('#typeSelect');
		      //执行重载
		      table.reload('idTest', {
		        page: {
		          curr: 1 //重新从第 1 页开始
		        }
		        ,where: {
		        	snTypeId:typeSelect.val()
		        }
		      });
		    }
		  };
		  
		  $('.demoTable .layui-btn').on('click', function(){
		    var type = $(this).data('type');
		    active[type] ? active[type].call(this) : '';
		  });
		  
		  $('#insert').on('click', function(){
			  layer.open({
	    	        type: 1	 //此处以iframe举例
	    	        ,title: '数据增加'
	    	        ,offset: 'auto'
	    	        ,area: ['550px', '350px']
	    	        ,content:$('#addOrUpdate')
	    	        ,end : function() {
	    	        	$("#operUser").attr({"disabled":false});
	    	        	//弹框关闭的回调函数
	    				$("#addOrUpdate").hide();
	    				exit();
	    			}
	    	    });
		  });
		  $('#sub').on('click', function(){
			  findmeTypeId();
			  findmeName();
			  
			  var countyz=null;
				$.each($(".clyz"),function(index,item){
					if($(item).val()=="√"){
						countyz++;
					}
			  });
			if(countyz>1){
			  var url="../../comtype/addOrUpdate.action";
			  var data=$("#myform").serialize();
			  $.post(url,data,function(mes){
				  if(mes==1){
					  exit();
					  $("#addOrUpdate").hide();
					  parent.layer.msg('操作成功', {
					      icon: 1
					  });
					  //重载表单
					  active['reload'] ? active['reload'].call(this) : '';
				  }else{
					  parent.layer.msg('操作失败', {
					      icon: 2
					  });
				  }
				  load();
			  },"json");
			}
		});
});

function findmeTypeId(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		
		if($("#snTypeId").val()==null || $("#snTypeId").val()==''){
			 parent.layer.msg('上级类型不能为空', {
			      icon: 5,
			      anim :6
			  });
			$("#maComTypeId").val("×");
		}else{
			$("#maComTypeId").val("√");
		}

	});
}


function findmeName(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		
		if($("#typeName").val()==null || $("#typeName").val()==''){
			 parent.layer.msg('商品类型名字不能为空', {
			      icon: 5,
			      anim :6
			  });
			 $("#maComTypeName").val("×");
			 
		}else if($("#typeName").val().length>10){
			 parent.layer.msg('商品类型名字大于10', {
			      icon: 5,
			      anim :6
			  });
			$("#maComTypeName").val("×");
		}else{
			$("#maComTypeName").val("√");
		}

	});
}

function load() {
	 var form = layui.form
	 form.render();
}